﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NS_DBOP;

public partial class Flow_DocList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataSet fds = new DBoperate().ExecuteQuery("select * from Flow", 0, 0, "flow");
            foreach (DataRow dr in fds.Tables[0].Rows)
            {
                this.drpFlow.Items.Add(new ListItem(dr["Name"].ToString(), dr["ID"].ToString()));
            }
            this.drpFlow.Items.Insert(0, "请选择公文流程");

            DataSet bds = new DBoperate().ExecuteQuery("select * from Branch", 0, 0, "branch");
            foreach (DataRow dr in bds.Tables[0].Rows)
            {
                this.drpBranch.Items.Add(new ListItem(dr["Branch"].ToString(), dr["Dutyid"].ToString()));
            }
            this.drpBranch.Items.Insert(0, "全部");

            this.drpUser.Items.Insert(0, "请选择人员");

            Bind();
        }

    }
    public void Bind()
    {
        //int userid = Convert.ToInt32(new DBoperate().ExecuteScalar("select Employeeid from Employee where username='" + Session["UserName"] + "'"));
        DataSet ds = new DBoperate().ExecuteQuery("select * from FlowDoc ", 0, 0, "table1");
        this.lblCount.Text = "共" + ds.Tables[0].Rows.Count + "条记录";
        DataView dv = ds.Tables[0].DefaultView;
        PagedDataSource pds = new PagedDataSource();
        AspNetPager1.RecordCount = dv.Count;
        pds.DataSource = dv;
        pds.AllowPaging = true;
        pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
        pds.PageSize = AspNetPager1.PageSize;
        this.GridView1.DataSource = pds;
        this.GridView1.DataBind();
    }

    public string getFlow(string id)
    {
        string flow = new DBoperate().ExecuteScalar("select Name from Flow where ID=" + Convert.ToInt32(id)).ToString();
        return flow;
    }

    public string getStep(string id)
    {
        string step = null;
        int end = Convert.ToInt32(new DBoperate().ExecuteScalar("select IsEnd from FlowDoc where ID=" + Convert.ToInt32(id)));
        if (end > 0)
        {
            step = "完成";
        }
        else
        {

            DataSet ds = new DBoperate().ExecuteQuery("select * from FlowPath where DocID=" + Convert.ToInt32(id), 0, 0, "action");
            //Response.Write("<script>alert('" + dr["IsApprove"].ToString() + "')</script>");
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (Convert.ToInt32(dr["IsApprove"]) != 2)
                {
                    string sql = "select Name from FlowAction where ID in (select ActionId from FlowStep where Num=" + Convert.ToInt32(dr["Num"]) + " and FlowID=" + Convert.ToInt32(dr["FlowID"]) + ")";
                    step = new DBoperate().ExecuteScalar(sql).ToString();

                    break;
                }
            }
        }
        return step;
    }
    public string getUser(string id)
    {
        string user = new DBoperate().ExecuteScalar("select Name from Employee where Employeeid=" + Convert.ToInt32(id)).ToString();
        return user;
    }
   
    protected void btnDel_Click(object sender, EventArgs e)
    {
        try
        {

            bool blSelected = false;
            int j = this.GridView1.Rows.Count;
            for (int i = 0; i < j; i++)
            {
                CheckBox ckTemp = (CheckBox)GridView1.Rows[i].FindControl("chkDo");
                if (ckTemp.Checked)
                {
                    blSelected = true;
                    string strId = this.GridView1.DataKeys[i]["ID"].ToString();

                    string strDel = "delete from FlowDoc where ID=" + strId;
                    int result = new DBoperate().ExecuteUpdate(strDel);


                }
            }
            if (blSelected == false)
            {
                Response.Write("<script language=javascript>alert('请先选择一条要操作的信息!');</script>");
            }

            Bind();//重新绑定数据
        }
        catch 
        {
            Response.Write("<script language=javascript>alert('错误！');</script>");
        }
        

    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Bind();
    }
    protected void bnQuery_ServerClick(object sender, EventArgs e)
    {

    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {

            CheckBox ckTemp = (CheckBox)row.FindControl("chkDo");
            if (ckTemp != null)
            {
                if (chkAll.Checked)
                {
                    ckTemp.Checked = true;
                }
                else
                {
                    ckTemp.Checked = false;
                }
            }
            else
            {
                Response.Write("<script language=javascript>alert('没有值');</script>");

            }

        }
    }

    protected void btnFind_Click(object sender, EventArgs e)
    {
        string strSql = "select * from FlowDoc where 1=1";
        string flow=this.drpFlow.SelectedItem.Value;
        string user=this.drpUser.SelectedItem.Value;
        string date=this.txtDate.Text;
        string title=this.txtTitle.Text;
        if (!this.drpFlow.SelectedItem.Text.Equals("请选择公文流程"))
        {
            strSql += " and FlowID=" + Convert.ToInt32(flow);
        }
        if (!this.drpUser.SelectedItem.Text.Equals("请选择人员"))
        {
            strSql += " and UserID=" + Convert.ToInt32(user);
        }
        if (!date.Equals(""))
        {
            //strSql += " and datediff(dd," + Convert.ToDateTime(date) + ",PubDate)=0";
        }
        if (!title.Equals(""))
        {
                strSql += " and Title like '%" + title + "%'";
        }

        DataSet ds = new DBoperate().ExecuteQuery(strSql, 0, 0, "flowdoc");
        this.GridView1.DataSource = ds.Tables[0];
        this.GridView1.DataBind();
        this.lblCount.Text = "共" + ds.Tables[0].Rows.Count + "条记录";
    }

    protected void drpBranch_SelectedIndexChanged(object sender, EventArgs e)
    {
        int  brid=Convert.ToInt32(this.drpBranch.SelectedItem.Value);
        DataSet uds = new DBoperate().ExecuteQuery("select * from Employee where dutyid=" + brid, 0, 0, "employee");
        this.drpUser.DataSource = uds.Tables[0];
        this.drpUser.DataTextField = "Name";
        this.drpUser.DataValueField = "Employeeid";
        this.drpUser.DataBind();
        
    }
}
